import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter_yishop/config/Config.dart';
import 'package:flutter_yishop/config/EventBus.dart';
import 'package:flutter_yishop/constants/Constants.dart';
import 'package:flutter_yishop/utils/screen/ScreenAdapter.dart';
import 'package:flutter_yishop/utils/toast_util.dart';
import 'package:flutter_yishop/widget/JDButton.dart';
import 'package:flutter_yishop/widget/JdText.dart';
import 'package:sp_util/sp_util.dart';

class LoginPage extends StatefulWidget {

  _LoginPageState createState() => _LoginPageState();
}

class _LoginPageState extends State<LoginPage> {
  ///注册用户  19234567890  123456
  String username= "";
  String password = "";

  @override
  Widget build(BuildContext context) {
    ScreenAdapter.init(context);
    return Scaffold(
      appBar: AppBar(
        title: Text("登录页面"),
        actions: <Widget>[
          FlatButton(
            child: Text("客服"),
            onPressed: () {
              ToastUtil.showToasts("客服");
            },
          )
        ],
      ),
      body: Container(
        padding: EdgeInsets.all(ScreenAdapter.width(20)),
        child: ListView(
          children: <Widget>[
            Center(
              child: Container(
                margin: EdgeInsets.only(top: 30),
                height: ScreenAdapter.width(160),
                width: ScreenAdapter.width(160),
                // child: Image.asset('images/login.png'),
                child: Image.asset('assets/images/ic_shop.png'),
                // Image.network(
                //     'https://www.itying.com/images/flutter/list5.jpg',
                //     fit: BoxFit.cover),
              ),
            ),
            SizedBox(height: 30),
            JdText(
              text: "请输入用户名",
              onChanged: (value) {
               username= value;
              },
            ),
            SizedBox(height: 10),
            JdText(
              text: "请输入密码",
              password: true,
              onChanged: (value) {
               password = value;
              },
            ),

            SizedBox(height: 10),

            Container(
              padding: EdgeInsets.all(ScreenAdapter.width(20)),
              child: Stack(
                children: <Widget>[
                  Align(
                    alignment: Alignment.centerLeft,
                    child: GestureDetector(
                      child: Text('忘记密码'),
                      onTap: (){
                        ToastUtil.showToasts("忘记密码");
                      },
                    ),
                  ),
                  Align(
                    alignment: Alignment.centerRight,
                    child: InkWell(
                      onTap: (){
                        Navigator.pushNamed(context, Constants.REGISTER);
                      },
                      child: Text('新用户注册'),
                    ),
                  )
                ],
              ),
            ),

           SizedBox(height: 20),
            JdButton(
              text:"登录",
              color: Colors.red,
              height: 74,
              cb: (){
                  doLogin();
              },
            )
          ],
        ),
      ),
    );
  }

  doLogin() async{
    RegExp reg = RegExp(r"^1\d{10}$");
    if(!reg.hasMatch(this.username)){
      ToastUtil.showToasts("手机号格式不对");
    }else if(password.length<6){
      ToastUtil.showToasts("密码错误");
    }else{
      var api = "${Config.url}api/doLogin";
      var response = await Dio().post(api,data: {"username":username,"password":password});
      if(response.data["success"]){
        print(response.data);
        SpUtil.putObjectList(Constants.USERINFO, response.data["userinfo"]);
        Navigator.pop(context);
      }else{
        ToastUtil.showToasts(response.data["message"]);
      }
    }
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    eventBus.fire(new UserEvent("登录成功"));
  }
}
